package com.lanjiyin.cloud.common.util.security.coder;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.lanjiyin.cloud.common.util.security.MD5;

/**
 * AESCoder
 * @author tudaojun
 *
 */
public class AESCoder {
	/**
	 * 密钥算法
	 */
	private static final String ALGORITHM = "AES";
	/**
	 * 加解密算法/工作模式/填充方式
	 */
	private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";
	
	static {
		//Security.addProvider(new BouncyCastleProvider());
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
	}
	
	/**
	 * AES加密
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String encryptData(String data,String key) throws Exception {
		// 创建密码器
		Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");
		SecretKeySpec secretKeySpec = new SecretKeySpec(MD5.md5(key).getBytes(), ALGORITHM);
		// 初始化
		cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
		return new String(Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes())));
	}
 
	/**
	 * AES解密
	 * 
	 * @param base64Data
	 * @return
	 * @throws Exception
	 */
	public static String decryptData(String base64Data,String key) throws Exception {
		Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");
		SecretKeySpec secretKeySpec = new SecretKeySpec(MD5.md5(key).getBytes(), ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
		return new String(cipher.doFinal(Base64.getDecoder().decode(base64Data)));
	}
 
	public static void main(String[] args) throws Exception {
		
//		String A="wvm81DNoEyMiBbF7gjHh51JWMpdVsxwGuQ/Ej4DcGR408/bJzsu0zsCGcm9t3OMuDCoI+oXQKZBX/iBFo/+TfOf0w8YMbeDZELHUBShN+Pw7It0rzsBY0/ScN9EFRAg9uFE5AKFzlEMdDnHdEk5IX8bn+q/iNyuZ20Zfatx/n1u1r/+IDKtrxUDyPpRxzeHlc0eIzN7tNfnW+BjWenmlK+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